package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.DfAppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.DfApp;
import com.ruoyi.system.service.IDfAppService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 应用Controller
 *
 * @author wangsr
 * @date 2022-01-13
 */
@RestController
@RequestMapping("/app")
public class DfAppController extends BaseController {
    @Autowired
    private IDfAppService dfAppService;

    /**
     * 查询应用列表
     */
    @RequiresPermissions("system:app:list")
    @GetMapping("/list")
    public TableDataInfo list(DfApp dfApp) {
        startPage();
        List<DfApp> list = dfAppService.selectDfAppList(dfApp);
        return getDataTable(list);
    }

    /**
     * 导出应用列表
     */
    @RequiresPermissions("system:app:export")
    @Log(title = "应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DfApp dfApp) {
        List<DfApp> list = dfAppService.selectDfAppList(dfApp);
        ExcelUtil<DfApp> util = new ExcelUtil<DfApp>(DfApp.class);
        util.exportExcel(response, list, "应用数据");
    }

    /**
     * 获取应用详细信息
     */
    @RequiresPermissions("system:app:query")
    @GetMapping(value = "/{appId}")
    public AjaxResult getInfo(@PathVariable("appId") Long appId) {
        return AjaxResult.success(dfAppService.selectDfAppByAppId(appId));
    }

    /**
     * 新增应用
     */
    @RequiresPermissions("system:app:add")
    @Log(title = "应用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DfApp dfApp) {
        return toAjax(dfAppService.insertDfApp(dfApp));
    }

    /**
     * 修改应用
     */
    @RequiresPermissions("system:app:edit")
    @Log(title = "应用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DfApp dfApp) {
        return toAjax(dfAppService.updateDfApp(dfApp));
    }

    /**
     * 删除应用
     */
    @RequiresPermissions("system:app:remove")
    @Log(title = "应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appIds}")
    public AjaxResult remove(@PathVariable Long[] appIds) {
        return toAjax(dfAppService.deleteDfAppByAppIds(appIds));
    }



    /**
     * 查询应用列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(DfApp dfApp) {
        //根据id查询
        Long userId = SecurityUtils.getUserId();
        if (userId != 1){
            dfApp.setUserId(userId);
        }
        List<DfApp> list = dfAppService.selectDfAppList(dfApp);
        return AjaxResult.success(list);
    }


    @GetMapping("/appUserList/{appId}")
    public AjaxResult appUserList(@PathVariable Long appId){
        DfApp dfApp = dfAppService.selectDfAppByAppId(appId);
        if (dfApp == null){
            throw new RuntimeException("应用不存在");
        }
        //查询门店人员
        List<SysUser> appUserList = dfAppService.selectDfAppUserByAppId(appId);
        return AjaxResult.success(appUserList);
    }


    /**
     * 查询未分配应用列表
     */
    @GetMapping("/appUserList/unallocatedList")
    public TableDataInfo unallocatedList(DfAppUser dfApp) {
        startPage();
        List<SysUser> list = dfAppService.selectUnallocatedList(dfApp);
        return getDataTable(list);
    }

}
